//
//  MPTMeHeadView.m
//  MPTLive
//
//  Created by caixun on 15/8/24.
//  Copyright (c) 2015年 Jeakin. All rights reserved.
//

#import "MPTMeHeadView.h"
#import "UIColor+MPTColor.h"
#import "MPTLoginUser.h"
#import "UIImageView+WebCache.h"
#import "MPTLiveCustomLabel.h"
#import "MPTUserView.h"
#import "MPTTool.h"
#import "UIFont+MPTFont.h"


@interface MPTMeHeadView ()

// 用户昵称
@property (nonatomic, strong) UILabel *nickNameLab;

// 性别
@property (nonatomic, strong) UIImageView *sexImgv;

// 用户宣言
@property (nonatomic, strong) UILabel *manifestoLab;

// 编辑个人资料
@property (nonatomic, strong) UIButton *editUserBtn;

// 用户头像模糊效果
@property (nonatomic, strong) UIImageView *viewBg;

// 最下面分割线
@property (nonatomic, strong) UIView *line;

/// 中间分割线
@property (nonatomic, strong) UIView *lineC;

//
@property (nonatomic, strong) MPTLiveCustomLabel *userNum;

//
@property (nonatomic, strong) MPTLiveCustomLabel *likeNum;

//
@property (nonatomic, strong) MPTUserView *mptUserView;

@end


@implementation MPTMeHeadView

/**
 *  alloc页面中的控件
 */
- (void)initViewContent
{
    self.backgroundColor = [UIColor colorWithRed:238/255.0f green:238/255.0f blue:238/255.0f alpha:1.0f];
    
    // 用户头像作为背景颜色 模态效果
    self.viewBg = [[UIImageView alloc] init];
    self.viewBg.clipsToBounds = YES;
    self.viewBg.alpha = 0.2;
    [self addSubview:self.viewBg];
    
    UIImage *defUserHead = [UIImage imageNamed:@"UserDefHead"];
    UIView  *view = [[UIView alloc] initWithFrame:CGRectMake((screenW-80)/2 - 2,
                                                             80-10-7,
                                                             defUserHead.size.width + 4,
                                                             defUserHead.size.height + 4)];
    view.backgroundColor = [UIColor whiteColor];
    view.layer.cornerRadius = view.frame.size.height / 2.0;
    [self addSubview:view];
    
    self.mptUserView = [[MPTUserView alloc] initWithFrame:CGRectMake((screenW-80)/2,
                                                                     80-10-5,
                                                                     defUserHead.size.width,
                                                                     defUserHead.size.height)];
    self.mptUserView.viewType = MPTUserViewType_SB;
    [self.mptUserView updateLoginUserInfo:[MPTLoginUser unArchiver]];
    [self addSubview:self.mptUserView];
     
    // 一个透明底色的button，点击该按钮时跳转到编辑个人资料页面
    self.editUserBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:self.editUserBtn];
    
    // 用户昵称
    self.nickNameLab = [[UILabel alloc] init];
    self.nickNameLab.font = [UIFont YXFontOfSize:17.0f];
    self.nickNameLab.textColor = [UIColor YXColorWithHexCode:@"666666"];
    [self addSubview:self.nickNameLab];
    
    // 性别
    self.sexImgv = [[UIImageView alloc] init];
    [self addSubview:self.sexImgv];
    
    // 观众数量
    self.userNum = [[MPTLiveCustomLabel alloc] init];
    self.userNum.textAlignment = NSTextAlignmentRight;
    [self addSubview:self.userNum];
    
    // 点赞数量
    self.likeNum = [[MPTLiveCustomLabel alloc] init];
    self.likeNum.textAlignment = NSTextAlignmentLeft;
    [self addSubview:self.likeNum];
    
    // 用户宣言
    self.manifestoLab = [[UILabel alloc] init];
    self.manifestoLab.textAlignment = NSTextAlignmentCenter;
    self.manifestoLab.font = [UIFont YXFontOfSize:12.0f];
    self.manifestoLab.numberOfLines = 0;
    [self.manifestoLab setTextColor:[UIColor YXColorWithHexCode:@"666666"]];
    [self addSubview:self.manifestoLab];
    
    self.line= [[UIView alloc] init];
    [self.line setBackgroundColor:[UIColor YXColorWithHexCode:@"D2D2D2"]];
    [self addSubview:self.line];
    
    self.lineC = [[UIView alloc] init];
    [self.lineC setBackgroundColor:[UIColor blackColor]];
     self.lineC.alpha = 0.2;
    [self addSubview:self.lineC];
}

/**
 *  图片模态效果
 *
 *  @param targetImage 要做模糊的图片
 *
 *  @return 返回模糊之后的图片
 */
- (UIImage *)imageFilter:(UIImage *)targetImage
{
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *inputImage = [[CIImage alloc] initWithImage:targetImage];
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [filter setValue:inputImage forKey:kCIInputImageKey];
    [filter setValue:[NSNumber numberWithFloat:8.0] forKey:@"inputRadius"];
    CIImage *result = [filter valueForKey:kCIOutputImageKey];
    CGRect extent = CGRectInset(filter.outputImage.extent, 25, 25);
    CGImageRef cgImage = [context createCGImage:result fromRect:extent];
    UIImage *image = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
    
    return image;
}

- (void)setTargetForBtn:(id)target
         withEditAction:(SEL)editAction
{
    [self.editUserBtn addTarget:target action:editAction forControlEvents:UIControlEventTouchUpInside];
}

- (void)setViewContent
{
    // 用户头像
    UIImage *defUserHead = [UIImage imageNamed:@"UserDefHead"];
    
    [self.mptUserView updateLoginUserInfo:[MPTLoginUser unArchiver]];
    self.viewBg.frame = CGRectMake(self.mptUserView.frame.origin.x,
                                   self.mptUserView.frame.origin.y,
                                   defUserHead.size.width,
                                   defUserHead.size.height);
    [self.viewBg sd_setImageWithURL:[NSURL URLWithString:[MPTLoginUser unArchiver].icon]
                   placeholderImage:defUserHead
                          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
     {
         if(image != nil)
         {
             self.viewBg.frame = self.frame;
             self.viewBg.contentMode = UIViewContentModeScaleAspectFill;
             self.viewBg.image = [self imageFilter:image];
         }
     }];
    
    // 一个透明底色的button，点击该按钮时跳转到编辑个人资料页面
    self.editUserBtn.frame = self.mptUserView.frame;
    [self.editUserBtn setCenter:self.mptUserView.center];
    
    // 用户昵称
    self.nickNameLab.frame = CGRectMake(0, 0, MAXFLOAT, 22);
    self.nickNameLab.text = [MPTLoginUser unArchiver].nick;
    [self.nickNameLab sizeToFit];
    self.nickNameLab.frame = CGRectMake((screenW-self.nickNameLab.frame.size.width)/2 - 7,
                                        self.mptUserView.frame.origin.y+self.mptUserView.frame.size.height+10,
                                        self.nickNameLab.frame.size.width,
                                        self.nickNameLab.frame.size.height);
    
    // 性别
    UIImage *sexManImage = [UIImage imageNamed:@"MPTCommonMan"];
    UIImage *sexWomanImage = [UIImage imageNamed:@"MPTCommonWoman"];
    self.sexImgv.frame = CGRectMake(self.nickNameLab.frame.origin.x+self.nickNameLab.frame.size.width+5,
                                    self.nickNameLab.frame.origin.y,
                                    sexManImage.size.width,
                                    sexManImage.size.height);
    if([MPTLoginUser unArchiver].gender.intValue == 1)
    {
        [self.sexImgv setImage:sexManImage];
    }
    else
    {
        [self.sexImgv setImage:sexWomanImage];
    }
    self.sexImgv.center = CGPointMake(self.sexImgv.center.x, self.nickNameLab.center.y);
    
    self.userNum.frame = CGRectMake(0,
                                    self.nickNameLab.frame.origin.y+self.nickNameLab.frame.size.height+8+3,
                                    screenW/2-10,
                                    20);
    self.likeNum.frame = CGRectMake(self.userNum.frame.size.width+10+10,
                                    self.userNum.frame.origin.y,
                                    screenW/2-10,
                                    18);
    
    // 用户宣言
    self.manifestoLab.frame = CGRectMake(0,
                                         0,
                                         screenW-20,
                                         MAXFLOAT);
    self.manifestoLab.text = [MPTLoginUser unArchiver].desc;
    [self.manifestoLab sizeToFit];
    self.manifestoLab.frame = CGRectMake(10,
                                         self.userNum.frame.origin.y+self.userNum.frame.size.height+7,
                                         screenW-20,
                                         self.manifestoLab.frame.size.height);
    self.manifestoLab.textAlignment = NSTextAlignmentCenter;
    
    /// headview 最下面的线
    self.line.frame = CGRectMake(0, self.frame.size.height, screenW, 0.5);
    [self.line setBackgroundColor:[UIColor YXColorWithHexCode:@"D2D2D2"]];
     
}

- (void)setUserAndLikeNum:(NSString *)userNum withLikeNum:(NSString *)likeNum
{
    //
    self.userNum.text = [NSString stringWithFormat:@"%@ 观众",[MPTTool getNumWith:userNum.intValue]];
    [self.userNum setFont:[UIFont YXFontOfSize:14.0f]
                fromIndex:0
                   length:self.userNum.text.length];
    [self.userNum setFont:[UIFont YXFontOfSize:14.0f]
                fromIndex:0
                   length:userNum.length];
    
    [self.userNum setColor:[UIColor YXColorWithHexCode:@"#666666"]
                fromIndex:0
                   length:self.userNum.text.length];
    [self.userNum setColor:[UIColor YXColorWithHexCode:@"#282828"]
                fromIndex:0
                   length:userNum.length];
    
    //
    self.likeNum.text = [NSString stringWithFormat:@"%@ 赞",[MPTTool getNumWith:likeNum.intValue]];
    [self.likeNum setFont:[UIFont YXFontOfSize:14.0f]
                fromIndex:0
                   length:self.likeNum.text.length];
    [self.likeNum setFont:[UIFont YXFontOfSize:14.0f]
                fromIndex:0
                   length:likeNum.length];
    
    [self.likeNum setColor:[UIColor YXColorWithHexCode:@"#666666"]
                fromIndex:0
                   length:self.likeNum.text.length];
    [self.likeNum setColor:[UIColor YXColorWithHexCode:@"#282828"]
                fromIndex:0
                   length:likeNum.length];
    
    self.lineC.frame = CGRectMake(screenW / 2.0, self.likeNum.frame.origin.y + 2, 0.5, 12);
}

+ (float)getHeadViewH
{
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0,
                                                             0,
                                                             screenW-20,
                                                             MAXFLOAT)];
    lab.text = [MPTLoginUser unArchiver].desc;
    lab.textAlignment = NSTextAlignmentCenter;
    lab.font = [UIFont systemFontOfSize:12.0f];
    [lab sizeToFit];
    [lab setTextColor:[UIColor YXColorWithHexCode:@"666666"]];
    
    // +7+7:manifestoLab和userNum之间的间距
    return 232-15+lab.frame.size.height + 7+7;
}


@end
